package controller;

import app.UpdateApp;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import model.VersionBean;
import sun.applet.Main;
import utils.Global;
import utils.MonologUtils;
import utils.UpdateTool;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.ResourceBundle;

public class Controller implements Initializable {
    public static Controller controller;

    @FXML
    private VBox leftBox;

    @FXML
    private AnchorPane rightBox;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        for(String name : Global.configBeanMap.keySet()){
            addLabel(name, false);
        }
        controller = this;
    }

    public void reName(String oldName, String newName){
        for(Node node: leftBox.getChildren()){
            if(node.getId().equals(oldName)){
                node.setId(oldName);
                ((Label)node).setText(newName);
            }
        }
    }

    private void addLabel(final String name, boolean isShow){
        Label label = new Label(name);
        label.setId(name);
        label.setPrefHeight(40);
        label.setFont(new Font(20));

        label.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>(){

            @Override
            public void handle(MouseEvent mouseEvent) {
                showRight(name);
            }
        });

        leftBox.getChildren().add(label);

        if(isShow){
            showRight(name);
        }
    }

    private void showRight(final String name){
        try {
            Parent root = FXMLLoader.load(Main.class.getResource("/xml/rightPanel.fxml"),
                    new ResourceBundle() {
                        @Override
                        protected Object handleGetObject(String key) {
                            if("name".equals(key)){
                                return name;
                            }
                            return "";
                        }

                        @Override
                        public Enumeration<String> getKeys() {
                            return null;
                        }
                    });
            root.getChildrenUnmodifiable();
            rightBox.getChildren().setAll(root);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void newConfig(ActionEvent actionEvent) {
//        MonologUtils.alert(getClass().getResource("").);
        String name = "unname"+ Global.getNumber();
        addLabel(name, true);
    }


    public void updateExe(ActionEvent actionEvent) {
        Platform.runLater(new Runnable() {
            public void run() {
                VersionBean newVersion = UpdateTool.checkUpdate();
                if(newVersion != null){
                    MonologUtils.alert("将更新到"+ newVersion.getCurVersion());
                    Platform.runLater(new Runnable() {
                        public void run() {
                            try {
                                new UpdateApp().start(new Stage());
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            }
        });

//
//        String root = UpdateTool.getProjectFolderPath();
//        //获取旧版本信息
//        File versionFile = new File(root, Constant.Version_File_Name);
//        VersionBean oldBean = FileUtils.readVersionBean(versionFile);
//        if(oldBean==null){
//            MonologUtils.alert("解析版本文件错误，无法更新");
//            return;
//        }
//        //获取新版本信息
//        File newVersionFile = new File(root, Constant.Update_Dir_Name+"/"+ Constant.Version_File_Name);
//        FileUtils.downLoadFile(newVersionFile);
//        VersionBean newBean = FileUtils.readVersionBean(newVersionFile);
//
//        if(oldBean.getCurVersion().equals(newBean.getCurVersion())){
//            MonologUtils.alert("您的版本已经是最新的");
//            return;
//        }
//
//        MonologUtils.alert("将更新到"+ newBean.getCurVersion());
//        Platform.runLater(new Runnable() {
//            public void run() {
//                try {
//                    new UpdateApp().start(new Stage());
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        });

//            Process p = Runtime.getRuntime().exec(Constant.Update_Exe_Name);
        //        Platform.exit();
    }
}
